/**
   * 规范树表数据
   * @param {Array} treeData 树表数据
   * @param {string} nodeKey 字典值，多个值以“,”隔开
   * @param {string||Array} topKey 顶层节点配对值，默认 0 ； 数组格式['0','1',...Array ]
   * @returns 规范后的树表数据
   */
function TreeSpecification(Array,nodeKey='pId',topKey=0){
    	// 调用方法， temp为原始数据, result为树形结构数据

	// 开始递归方法
	function generateOptions(params) {
      let result = []
      for (const param of params) {
        param.value = param.id
        param.label = param.title || param.name
        if(param?.[nodeKey]){
          console.log(typeof topKey)
            let pushState = false
            if(typeof topKey == 'number'||typeof topKey == 'string'){
              if (param?.[nodeKey] == 0) {  // 判断是否为顶层节点
                pushState=true
              }
            }else if(typeof topKey == 'object'){
              if (topKey.indexOf(param?.[nodeKey])>-1) {  // 判断是否为顶层节点
                pushState=true
              }
            }

            if(pushState){
              let parent = param
              parent.children = getchilds(param.id, params)  // 获取子节点
              result.push(parent)
            }
        }
      }
      return result
    }

    function getchilds(id, array) {
      const childs = []
      for (const arr of array) {  // 循环获取子节点
        if(arr?.[nodeKey]){
            if (arr[nodeKey] === id) {
                childs.push(arr)
            }
        }
      }
      for (const child of childs) { // 获取子节点的子节点
        const childscopy = getchilds(child.id, array)// 递归获取子节点
        if (childscopy.length > 0) {
          child.children = childscopy
        }
      }
      return childs
    }

    return generateOptions(Array)

}

export default {
    TreeSpecification
}